package com.wjh.kt_vod.exception;

import com.wjh.kt_vod.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail(null).message("执行全局异常处理");
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error() {
        return Result.fail().message("执行特定异常 ArithmeticException");
    }

//    GgkException

    /**
     * 自定义异常
     */
    @ExceptionHandler(GgkException.class)
    @ResponseBody
    public Result error(GgkException e) {
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }

    /**
     * 自定义异常
     */
    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public Result error(NumberFormatException e) {
        return Result.fail(null).message("类型转换异常");
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result error(NullPointerException e) {
        return Result.fail(null).message("空指针异常");
    }

}
